/*
 * @Description: 
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2021-04-20 11:11:42
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-21 17:12:22
 */
class Observer {
    constructor(data) {
        this.walk(data)
    }
    walk(data) {
        if (!data || typeof (data) !== 'object') {
            return
        }
        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key])
        })
    }
    defineReactive(data, key, value) {
        const that = this
        // 如果value是一个对象 继续递归walk
        this.walk(value)
        // 依赖收集器
        const dep = new Dep()

        Object.defineProperty(data, key, {
            get() {
                Dep.target && dep.addSub(Dep.target)
                return value
            },
            set(newValue) {
                if (value === newValue) {
                    return
                }
                value = newValue
                that.walk(newValue)
                dep.notify()
            }
        })
    }
}